package com.example.vpdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager vp;
    private TabLayout tab;
    ArrayList<Fragment> fragments;
    ArrayList<String> titles;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private FrameLayout fl;
    FragmentC fragmentC;
    FragmentD fragmentD;
    private TextView tv_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main2);
        tv_img = (TextView)findViewById(R.id.tv_img);
        tv_img.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); //下划线
        vp = (ViewPager) findViewById(R.id.vp);
        tab = (TabLayout) findViewById(R.id.tab);

        fl = (FrameLayout) findViewById(R.id.fl);
        fragmentC = new FragmentC();
        fragmentD = new FragmentD();
        fragments = new ArrayList<>();
        fragments.add(fragmentC);
        fragments.add(fragmentD);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        titles= new ArrayList<>();
        titles.add("C");
        titles.add("D");


        FPAdater fpAdater = new FPAdater(getSupportFragmentManager(), fragments, titles);

        vp.setAdapter(fpAdater);
        tab.setupWithViewPager(vp);//与viewpager联系起来

//        addFragment(fragmentA);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn1:
//                addFragment(fragmentC);
//                break;
//            case R.id.btn2:
//                addFragment(fragmentD);
//                break;
//            case R.id.btn3:
//                break;
        }

    }

    //向Activity中添加Fragment的方法
    public void addFragment(Fragment fragment) {

        //获得Fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        //使用管理器开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //使用事务替换Fragment容器中Fragment对象
        fragmentTransaction.replace(R.id.fl, fragment);
        //提交事务，否则事务不生效
        fragmentTransaction.commit();
    }

}
